package xyz.yolin.service;

import com.google.common.base.Splitter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.yolin.mapper.AdminMapper;
import xyz.yolin.pojo.Admin;
import xyz.yolin.utils.BeanMapUtils;
import xyz.yolin.utils.MapParameter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
//    public void setAdminMapper(AdminMapper adminMapper){
//        this.adminMapper=adminMapper;
//    }

    @Override
    public int create(Admin admin) {
        System.out.println("还没插入数据时的admin"+admin);
        return adminMapper.create(admin);
    }


    @Override
    public int delete(String str) {
//        Map<String,Object> paramMap=new HashMap<String, Object>();
//        paramMap.put("id",id);
        List<String> list = Splitter.on(",").splitToList(str);
        int flag=0;
        for(String i:list){
            adminMapper.delete(MapParameter.getInstance().putId(Integer.parseInt(i)).getMap());
            flag++;
        }
        return flag;
    }


    @Override
    public int update(Admin admin) {
//        HashMap<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put("id",admin.getId());
//        paramMap.put("updateAccount",admin.getAccount());
//        paramMap.put("updateName",admin.getName());
//        paramMap.put("updatePassword",admin.getPassword());
//        paramMap.put("updateRemark",admin.getRemark());

        //将对象转换成map
//        Map<String, Object> map = BeanMapUtils.beanToMapForUpdate(admin);
//        map.put("id",admin.getId());

        //链式调用
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(admin))
                .putId(admin.getId()).getMap();
        System.out.println("更新操作执行的语句"+map);
        return adminMapper.update(map);
    }


    @Override
    public List<Admin> query(Admin admin) {
//        HashMap<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put("id",admin.getId());
//        paramMap.put("Account",admin.getAccount());
//        paramMap.put("Name",admin.getName());
//        paramMap.put("Password",admin.getPassword());
//        paramMap.put("Remark",admin.getRemark());

//        Map<String, Object> map = BeanMapUtils.beanToMap(admin);
//        System.out.println(map);

       Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(admin)).getMap();

        return adminMapper.query(map);
    }


    @Override
    public Admin detail(int id) {
//        HashMap<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put("id",id);

        Map<String, Object> map = MapParameter.getInstance().putId(id).getMap();
        System.out.println("明细detail："+map);
        return adminMapper.detail(map);
    }


    @Override
    public int count(Admin admin) {
//        HashMap<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put("id",admin.getId());
//        paramMap.put("Account",admin.getAccount());
//        paramMap.put("Name",admin.getName());
//        paramMap.put("Password",admin.getPassword());
//        paramMap.put("Remark",admin.getRemark());

//        Map<String, Object> map = BeanMapUtils.beanToMapForUpdate(admin);
//        System.out.println(map);

        return adminMapper.count(MapParameter.getInstance().put(BeanMapUtils.beanToMap(admin)).getMap());
    }

//    public Admin login(String account, String password) {
//        return null;
//    }

    @Override
    public Admin login(String account, String password){
        System.out.println(account);
        System.out.println(password);
        Map<String, Object> map = MapParameter.getInstance().put("account", account).put("password", password).getMap();
        System.out.println("---"+map);
        return adminMapper.detail(map);
    }
}
